﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.OleDb;
using System.IO;

namespace MailMerge_Generator
{
    public partial class Frm_Config1 : Form
    {
        int i;
        public Frm_Config1()
        {
            InitializeComponent();
            if (Frm_Main.oLetter.HasMerge && Frm_Main.oLetter.MergeFields.Count == 0)
            {
                PullColumnNames(Frm_Main.oLetter.MergeFilePath, lst_Mergefields); 
            }
            else
            {
                for (i = 0; i < Frm_Main.oLetter.MergeFields.Count; i++)
                {
                    lst_Mergefields.Items.Add(Frm_Main.oLetter.MergeFields[i]);
                } 
            }
            if (Frm_Main.oLetter.HasTable && Frm_Main.oLetter.TableFields.Count == 0)
            {
                PullColumnNames(Frm_Main.oLetter.TableFilePath, lst_Tablefields);
            }
            else
            {
                for (i = 0; i < Frm_Main.oLetter.TableFields.Count; i++)
                {
                    lst_Tablefields.Items.Add(Frm_Main.oLetter.TableFields[i]);
                } 
            }
            txt_TableskuID.Text = Frm_Main.oLetter.TableKey;
            txt_MergeskuID.Text = Frm_Main.oLetter.MergeKey;
            txt_MergeTableName.Text = Frm_Main.oLetter.MergeSource;
            txt_RoylatyTableName.Text = Frm_Main.oLetter.TableSource;
            lbl_mergeSheet.Text = "Chosen worksheet in " + Frm_Main.oLetter.Mergefilename();
            lbl_tableSheet.Text = "Chosen worksheet in " + Frm_Main.oLetter.Tablefilename();
        }
        public void PullColumnNames(String file, ListBox box)
        {
            OleDbConnection objConn = null;
            System.Data.DataTable dt = null;
            if (!File.Exists(file))
            {
                objConn = null;
            }
            else
            {
                FileInfo fileInfo = new FileInfo(file);
                objConn = new OleDbConnection((String)Utilities.GetConnectionString(file, true));
                objConn.Open();
                dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);
                if (dt == null)
                {
                    objConn = null;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    box.Items.Add(dt.Rows[i].ItemArray[3].ToString());
                }
            }
        }
        public void MoveItem(int direction, ListBox listBox1)
        {
            if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
                return;
            int newIndex = listBox1.SelectedIndex + direction;
            if (newIndex < 0 || newIndex >= listBox1.Items.Count) return;
            object selected = listBox1.SelectedItem;
            listBox1.Items.Remove(selected);
            listBox1.Items.Insert(newIndex, selected);
            listBox1.SetSelected(newIndex, true);
        }
        //************************
        //*****Button Methods*****
        //************************
        private void btn_AddMergeField_Click(object sender, EventArgs e)
        {
            lst_Mergefields.Items.Add(txt_AddMergeField.Text);
            txt_AddMergeField.Text = "";
        }
        private void btn_AddTableField_Click(object sender, EventArgs e)
        {
            lst_Tablefields.Items.Add(txt_AddTableField.Text);
            txt_AddTableField.Text = "";
        }
        private void btn_RemoveMergeField_Click(object sender, EventArgs e)
        {
            lst_Mergefields.Items.Remove(lst_Mergefields.SelectedItem);
        }
        private void btn_RemoveTableField_Click(object sender, EventArgs e)
        {
            lst_Tablefields.Items.Remove(lst_Tablefields.SelectedItem);
        }
        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btn_SaveFields_Click(object sender, EventArgs e)
        {
            int sizeMerge = lst_Mergefields.Items.Count;
            int sizeTable = lst_Tablefields.Items.Count;
            Frm_Main.oLetter.TableFields.Clear();
            Frm_Main.oLetter.MergeFields.Clear();
            for (i = 0; i < sizeMerge; i++)
            {
                Frm_Main.oLetter.MergeFields.Add(lst_Mergefields.Items[i].ToString());
            }
            for (i = 0; i < sizeTable; i++)
            {
                Frm_Main.oLetter.TableFields.Add(lst_Tablefields.Items[i].ToString());
            }
            Frm_Main.oLetter.MergeSource = txt_MergeTableName.Text;
            Frm_Main.oLetter.TableSource = txt_RoylatyTableName.Text;
            Frm_Main.oLetter.TableKey = txt_TableskuID.Text;
            Frm_Main.oLetter.MergeKey = txt_MergeskuID.Text;
            Close();
        }
        private void btn_TableItem_UP_Click(object sender, EventArgs e)
        {
            MoveItem(-1, lst_Tablefields);
        }
        private void btn_TableItem_DOWN_Click(object sender, EventArgs e)
        {
            MoveItem(1, lst_Tablefields);
        }
        private void btn_MergeItem_UP_Click(object sender, EventArgs e)
        {
            MoveItem(-1, lst_Mergefields);
        }
        private void btn_MergeItem_DOWN_Click(object sender, EventArgs e)
        {
            MoveItem(1, lst_Mergefields);
        }
    }
}
